package com.lire.shop.service.impl;

import com.google.gson.Gson;
import com.lire.shop.converter.OrderConverter;
import com.lire.shop.dao.OrderRepository;
import com.lire.shop.data.Constants;
import com.lire.shop.data.NullObjects;
import com.lire.shop.entity.Order;
import com.lire.shop.entity.Product;
import com.lire.shop.entity.User;
import com.lire.shop.entity.Wallet;
import com.lire.shop.model.vo.OrderVO;
import com.lire.shop.service.*;
import com.lire.shop.utils.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private Gson gson;
    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private ProductService productService;

    @Autowired
    private UserService userService;

    @Autowired
    private WalletService walletService;

    @Autowired
    private ChecklistService checklistService;

    @Autowired
    private OrderConverter orderConverter;


    /**
     * 购买商品业务
     * @param pid
     * @param session
     * @return
     */
    @Override
    public OrderVO buyProduct(Long pid, HttpSession session) {
        // 获取商品、用户、钱包信息
        Product product = productService.getProductById(pid);
        User user = userService.getCurrentUser(session);
        Wallet wallet = walletService.getWalletById(user.getWid());

        // 获取钱包余额、商品价格
        BigDecimal balances = wallet.getBalances();
        String price = product.getPrice();
        BigDecimal bdPrice = new BigDecimal(price);

        // 判断库存、余额
        if (product.getStock() <= 0 || balances.compareTo(bdPrice) < 0) {
            return null;
        }
        // 新增订单
        Order order = new Order();
        order.setUid(user.getUid());
        order.setPid(product.getPid());
        order.setOrderNumber(IDUtils.getUUID());
        order.setOrderStatus(Constants.ORDER_PAID_BEEN);
        orderRepository.save(order);

        // 更新钱包余额
        BigDecimal newBalances = balances.subtract(bdPrice);
        wallet.setBalances(newBalances);
        walletService.updateBalances(wallet);

        //更新商品库存
        product.setStock(product.getStock() - 1);
        productService.updateStock(product);

        //添加余额明细
        Long oid = orderRepository.findByOrderNumber(order.getOrderNumber()).getOid();
        checklistService.insertByOid(oid,Constants.WALLET_OUTLAY,newBalances.toString());

        return orderConverter.do2vo(order, user, product);
    }

    @Override
    public OrderVO refundProduct(Long oid, HttpSession session) {
        // 获取商品、用户、钱包、订单信息
        Order order = orderRepository.findById(oid).orElseGet(NullObjects::getOrder);
        Product product = productService.getProductById(order.getPid());
        User user = userService.getCurrentUser(session);
        Wallet wallet = walletService.getWalletById(user.getWid());

        //判断订单的有效性
        if (!order.getOrderStatus().equals(Constants.ORDER_PAID_BEEN)) return null;

        //更新订单状态
        order.setOrderStatus(Constants.ORDER_PAID_BACK);
        orderRepository.updateStatusById(order.getOrderStatus(), order.getOid());

        //更新钱包余额
        String price = product.getPrice();
        BigDecimal balances = wallet.getBalances();
        BigDecimal priceBd = new BigDecimal(price);
        BigDecimal newBalances = balances.add(priceBd);
        wallet.setBalances(newBalances);
        walletService.updateBalances(wallet);

        //增加余额明细
        checklistService.insertByOid(oid,Constants.WALLET_INCOME,newBalances.toString());

        return orderConverter.do2vo(order,user,product);
    }
}
